package com.suixinsanren.design.state.demo.demo06;

import java.util.Random;

public class HasQuarterState implements State {

    private final GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("已经有硬币了，请不要重复投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("硬币已退还");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("游戏开始");
        Random random = new Random();
        if (gumballMachine.getCount() > 0) {
            int winner = random.nextInt(10);
            System.out.println(winner);
            if (winner == 1) {
                System.out.println("恭喜您，您是幸运星");
                gumballMachine.setState(gumballMachine.getWinnerState());
            } else {
                gumballMachine.setState(gumballMachine.getSoldState());
            }
        } else {
            System.out.println("抱歉，糖果已售罄");
            gumballMachine.setState(gumballMachine.getSoldOutState());
        }
    }

    @Override
    public String toString() {
        return "已投入硬币";
    }
}
